<?php
/**                                          
 * 关注的分组
 * @author M2
 * @version 1.0
 * create Date 2013-04-27
 */
class FollowGroupModel extends Model{
	var $tableName = 'weibo_follow_group';
    const CACHE_PREFIX = "weibo_follow";

	/**
	 * 获取指定用户所有的关注的分组	
	 * @param int $uid 用户ID
	 */
	public function getGroupList($uid){
        if(!is_numeric($uid)) throw new ThinkException(L("arg_number_only"));
        	$follow_group_list = $this->where("uid={$uid}")->order('ctime ASC')->select();
		return $follow_group_list;
	}

	/**
	 * 获取指定用户指定关注的人所在分组	
	 * @param int $uid 用户ID 
	 * @param int $fid 好友ID
	 */
	public function getGroupStatus($uid,$fid){
		$map = array('uid' => intval($uid),'fid' => intval($fid),'type'=> 0);
		$follow_id = M('weibo_follow')->where($map)->getField('follow_id');
		if($follow_id){
			$follow_group_status = $this->field('link.follow_group_id AS gid,group.title')
										->table("{$this->tablePrefix}weibo_follow_group_link AS link LEFT JOIN {$this->tablePrefix}{$this->tableName} AS `group` ON link.follow_group_id=group.follow_group_id AND link.uid=group.uid")
										->where("link.follow_id={$follow_id} AND group.uid={$uid}")
										->order('group.follow_group_id ASC')
										->select();
			if(empty($follow_group_status))$follow_group_status[0] = array('gid'=>0,'title'=> '未分组');
			return $follow_group_status;
		}else{
			return false;
		}
	}

	/**
	 * 设置好友的分组状态	
	 * @param int $uid 用户ID
	 * @param int $fid 好友ID
	 * @param int $gid 分组ID
	 * @param int $action 操作类型
	 */
	public function setGroupStatus($uid,$fid,$gid,$action=NULL){
	    F(self::CACHE_PREFIX."list_".$uid,null);
	    F(self::CACHE_PREFIX."usergroup_{$uid}_{$gid}",null);
		$map = array('uid' => intval($uid),'fid' => intval($fid),'type'=> 0);
		$follow_id = M('weibo_follow')->where($map)->getField('follow_id');
		$gid	   = $this->where("uid={$map['uid']} AND follow_group_id={$gid}")->getField('follow_group_id');
		if($follow_id && $gid){
			$linkModel = M('weibo_follow_group_link');
			$data = array('follow_group_id'=> $gid,'follow_id'=>$follow_id,'uid'=> $map['uid']);
			if($action == NULL){
				$linkModel->where($data)->delete() || $linkModel->add($data);
			}elseif($action == 'add'){
				$linkModel->where($data)->find() || $linkModel->add($data);
			}elseif($action == 'delete'){
				$linkModel->where($data)->delete();
			}
		}
	}

	/**
	 * 添加/修改分组	
	 * @param int $uid 用户ID
	 * @param string $title 分组名
	 * @param int $gid 分组ID
	 */
	public function setGroup($uid,$title,$gid=NULL){
	    F(self::CACHE_PREFIX."list_".$uid,null);
	    F(self::CACHE_PREFIX."usergroup_{$uid}_{$gid}",null);
		$uid   = intval($uid);
		if(!$title)return 0;
		//查看分组是否已存在
		$map = array('uid'   => $uid,'title' => $title);
		$_gid = $this->where($map)->getField('follow_group_id');
		if(!$_gid){
			if($gid == NULL){
				$data = array('uid'   => $uid,'title' => $title,'ctime' => time());
				$gid = $this->add($data);
				return $gid;
			}else{
				$gid   = intval($gid);
				if(!$gid)return 0;
				$data = array('follow_group_id'   => $gid,'uid'   => $uid,'title' => $title);
				$res = $this->save($data);
				return 1;
			}
		}elseif($_gid == $gid){
			return 1;
		}else{
			return 0;
		}
	}

	/**
	 * 删除某个分组	
	 * @param int $uid 用户ID
	 * @param int $gid 分组ID
	 */
	public function deleteGroup($uid,$gid){
	    F(self::CACHE_PREFIX."list_".$uid,null);
	    F(self::CACHE_PREFIX."usergroup_{$uid}_{$gid}",null);
		$uid = intval($uid);
		$gid = intval($gid);
		$res = $this->where("uid={$uid} AND follow_group_id={$gid}")->delete();
		if($res){
			// 清除相应分组信息
			M('weibo_follow_group_link')->where("uid={$uid} AND follow_group_id={$gid}")->delete();
			return 1;
		}else{
			return 0;
		}
	}
	/**
	 * 获取指定用户指定分组下的关注的人的ID	
	 * @param int $uid 用户ID
	 * @param int $gid 分组ID
	 */
	public function getUsersByGroup($uid,$gid){
		$uid = intval($uid);
		$gid = intval($gid);
	    $follow_group_id_sql = $gid==0?' AND link.follow_group_id IS NULL':" AND link.follow_group_id={$gid}";
	    $fid = $this->field('follow.fid')
				    ->table("{$this->tablePrefix}weibo_follow AS `follow` LEFT JOIN {$this->tablePrefix}weibo_follow_group_link AS link ON follow.follow_id=link.follow_id AND follow.uid=link.uid")
				    ->where("follow.type=0 AND follow.uid={$uid}".$follow_group_id_sql)
				    ->select();
	    foreach($fid as $v){
	        $_fid[] = $v['fid'];
	    }
		return $_fid;
	}
}
?>